﻿var nextPageRequested = false;
var loadCompleted = true;
function next(q, p) {
    if (!loadCompleted) {
        nextPageRequested = true;
    }
    else {
        document.getElementById('content_prev').innerHTML = document.getElementById('content').innerHTML;
        document.getElementById('content').innerHTML = document.getElementById('content_next').innerHTML;
        document.getElementById('content_next').innerHTML = "Not loaded yet";
        loadCompleted = false;
        $('#content_next').load('/Home/SearchPartial', { 'q': q, 'p': p + 1 }, function () {
            loadCompleted = true;
            if (nextPageRequested) {
                next(q, p + 1);
                nextPageRequested = false;
            }
        });
    }
}

function prev(q, p) {
    if (!loadCompleted) {
        nextPageRequested = true;
    }
    else {
        document.getElementById('content_next').innerHTML = document.getElementById('content').innerHTML;
        document.getElementById('content').innerHTML = document.getElementById('content_prev').innerHTML;
        document.getElementById('content_prev').innerHTML = "Not loaded yet";
        loadCompleted = false;
        $('#content_prev').load('/Home/SearchPartial', { 'q': q, 'p': p - 1 }, function () {
            loadCompleted = true;
            if (nextPageRequested) {
                prev(q, p - 1);
                nextPageRequested = false;
            }
        });

    }
}
